/*****************************************************************************
	SNEGA UTILITIES 0.7a
	Copyright (C) 2009, Xiaohan Tian (snega0223)
	
	http://code.google.com/p/snega-utilities/
	
	This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License(LGPL) as
	published by the Free Software Foundation, either version 3 of the 
	License, or(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU Lesser General Public License for more details.
	
	You should have received a copy of the GNU Lesser General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*****************************************************************************/

package org.sng.util.db.elmt;

import java.sql.*;

import java.util.*;

import org.sng.util.slog.SLog;
import org.sng.util.var.*;
import org.sng.util.web.elmt.RuntimeEnvironment;

public class DataRow extends SVoid {
	
	private int m_status;
	private HashMap<String, DataCell> m_cells;
	
	public DataRow()
	{
		this.m_cells = new HashMap<String, DataCell>();
		
		this.m_status = DataRowStatus.NORMAL;
	}
	
	public DataRow(int n)
	{
		this.m_cells = new HashMap<String, DataCell>();
		
		this.m_status = DataRowStatus.NORMAL;
	}
	
	public int HasChanged()
	{
		return this.m_status;
	}
	
	public void Change()
	{
		this.m_status = DataRowStatus.CHANGED;
	}
	
	public void Remove()
	{
		this.m_status = DataRowStatus.REMOVED;
	}
	
	public void PushCell(String key, String value, int type)
	{
		if (value == null)
		{
			return;
		}
		
		try {
			switch (type) {
				case Types.INTEGER:
				{
					this.PushCell(key, Integer.valueOf(value));
				}
				case Types.DOUBLE:
				{
					this.PushCell(key, Double.valueOf(value));
				}
				default:
				{
					this.PushCell(key, value);
				}
			}
		} catch (Exception e) {
			SLog.println("Error in processing key : " + key + ", value : " + value);
		}
	}
	
	public void PushCell(String key, int value)
	{
		DataCellForInt cell = new DataCellForInt(this);
		
		cell.SetValue(value);
		
		this.m_cells.put(key, cell);
	}
	
	public void PushCell(String key, double value)
	{
		DataCellForDouble cell = new DataCellForDouble(this);
		
		cell.SetValue(value);
		
		this.m_cells.put(key, cell);
	}
	
	public void PushCell(String key, String value)
	{
		DataCellForString cell = new DataCellForString(this);
		
		cell.SetValue(value);
		
		this.m_cells.put(key, cell);
	}
	
	public void PushCell(String key, DataCell value)
	{
		this.m_cells.put(key, value);
	}
	
	public DataCell GetCell(String key)
	{
		return this.m_cells.get(key);
	}
	
	public DataCell RemoveCell(String key)
	{
		return this.m_cells.remove(key);
	}
	
	public int GetCellValueInt(String key)
	{
		try {
			return this.GetCell(key).GetInt();
		} catch (Exception e) {
			return 0;
		}
	}
	
	public String GetCellValueString(String key)
	{
		try {
			return this.GetCell(key).GetString();
		} catch (Exception e) {
			return "";
		}
		
	}
	
	// override methods
	public SVoid GetMember(RuntimeEnvironment in, String key)
	{
		return this.GetCell(key);
	}
	
	public SVoid GetMember(RuntimeEnvironment in, SVoid idx)
	{
		return this.GetCell(idx.GetString(in));
	}

}
